import { getTableStyleAPI, list as listAPI, save as saveAPI, delete8PrimaryKey } from '~/api/sysRole.js'

export default {
  namespaced: true,
  state(){
    return {
      tableStyle: [],
      list: []
    }
  },
  mutations:{
    setTableStyle(state, tableStyle){
      state.tableStyle = tableStyle
    },
    setList(state, list){
      state.list = list
    },
    delete8PrimaryKey(state, iid){
      state.list = state.list.filter(item => item.iid !== iid)
    }
  },
  actions:{
    getTableStyle({commit}){
      return new Promise(resolve => {
        getTableStyleAPI().then(res => {
          if(res.code !== 1) return
          commit('setTableStyle', res.data)
          resolve(res)
        })
      })
    },
    list({ commit }){
      return new Promise(resolve => {
        listAPI().then(res => {
          if(res.code !== 1 ) return
          commit('setList', res.data)
          resolve(res)
        })
      })
    },
    delete({ commit }, iid){
      return new Promise((resolve, reject) => {
        delete8PrimaryKey(iid).then(res => {
          if(res.code !== 1) return
          commit('delete8PrimaryKey', iid)
          resolve(res)
        })
      })
    },
    save({ commit }, {record, records, recordRowIndex}){
      return new Promise((resolve, reject) => {
        saveAPI(record).then(res => {
          if(res.code !== 1 ) return
          records.splice(recordRowIndex, 1, res.data)
          commit('setList', records)
          resolve(res)
        })
      })
    }
  }
}
